package com.easthome.excel;

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

public class ReadWriteExcel {
    public static void main(String[] args) throws IOException{
        writeExcel();
    }
    /**
     * 写入excel
     */
    private static void writeExcel()throws IOException{
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建工作簿
        XSSFSheet sheet = workbook.createSheet("我的工作簿");
        XSSFRow row0 = sheet.createRow(0);
        row0.createCell(0).setCellValue("姓名");
        row0.createCell(1).setCellValue("年龄");

        XSSFRow row1 = sheet.createRow(1);

        row1.createCell(0).setCellValue("张三");
        row1.createCell(1).setCellValue(22);



        /*//工作簿对象创建行
        XSSFRow row = sheet.createRow(0);
        //创建单元各
        XSSFCell cell = row.createCell(0);
        //写入单元格数据
        cell.setCellValue("姓名");*/

        //创建excel文件
        FileOutputStream fos = new FileOutputStream("d:/excel.xlsx");
        workbook.write(fos);

        fos.close();
        workbook.close();
    }


    /**
     * 读取excel
     */
    private static void readExcel()throws IOException {
        //创建Excel的操作类，构造方法传递excel文件路径
        XSSFWorkbook workbook = new XSSFWorkbook("d:/hello.xlsx");
        //workbook获取工作簿
        XSSFSheet sheet = workbook.getSheet("Sheet1");
        //获取最大行号，返回值是1，0行和1行
        int lastRowNum = sheet.getLastRowNum();
        System.out.println("lastRowNum = " + lastRowNum);
        for (int x = 0 ; x <= lastRowNum ; x++){
            //获取行
            XSSFRow row = sheet.getRow(x);
            //row行对象，获取列的最大值,返回值是2，0列和1列
            short lastCellNum = row.getLastCellNum();
            System.out.println("lastCellNum = " + lastCellNum);
            for (int y = 0 ;y < lastCellNum; y++){
                XSSFCell cell = row.getCell(y);
                //判断列的数据类型，是不是数字类型
                if (cell.getCellType() == CellType.NUMERIC){
                    double numericCellValue = cell.getNumericCellValue();
                    System.out.println("numericCellValue = " + numericCellValue);
                }
                else{
                    String stringCellValue = cell.getStringCellValue();
                    System.out.println("stringCellValue = " + stringCellValue);
                }
            }
        }
        workbook.close();

    }
}
/**
 *  //sheet工作簿对象，获取行，第0行，返回值对象
 *         XSSFRow row = sheet.getRow(0);
 *         //row行对象，获取列（单元格 cell）
 *         XSSFCell cell = row.getCell(0);
 *         //单元格对象，获取值
 *         String stringCellValue = cell.getStringCellValue();
 *         System.out.println("stringCellValue = " + stringCellValue);
 *
 *         XSSFCell cell1 = row.getCell(1);
 *         String stringCellValue1 = cell1.getStringCellValue();
 *         System.out.println("stringCellValue1 = " + stringCellValue1);
 *
 *         XSSFRow row1 = sheet.getRow(1);
 *         XSSFCell cell2 = row1.getCell(0);
 *         String stringCellValue2 = cell2.getStringCellValue();
 *         System.out.println("stringCellValue2 = " + stringCellValue2);
 *
 *         //年龄 20 是数字类型
 *         XSSFCell cell3 = row1.getCell(1);
 *         double stringCellValue3 = cell3.getNumericCellValue();
 *         System.out.println("stringCellValue3 = " + stringCellValue3);
 */